package com.rongji.dfish.demo.index.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.demo.base.DemoBaseController;
import com.rongji.dfish.demo.entity.PubLoginUser;
import com.rongji.dfish.demo.index.service.DemoService;
import com.rongji.dfish.demo.index.view.DemoView;
import com.rongji.dfish.demo.pub.service.UserService;
import com.rongji.dfish.demo.pub.view.PubView;
import com.rongji.dfish.framework.FrameworkHelper;
import com.rongji.dfish.ui.Widget;
import com.rongji.dfish.ui.command.CommandGroup;
import com.rongji.dfish.ui.command.DialogCommand;
import com.rongji.dfish.ui.command.JSCommand;
import com.rongji.dfish.ui.command.ReplaceCommand;
import com.rongji.dfish.ui.command.TipCommand;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.widget.DialogTemplate;

@Controller
@RequestMapping("/demo")
public class DemoController extends DemoBaseController {
	
	@Autowired
	private DemoService demoService;

	public DemoService getDemoService() {
		return demoService;
	}

	public void setDemoService(DemoService demoService) {
		this.demoService = demoService;
	}

	private static Map<String, DialogTemplate> dialogTemplates = new HashMap<String, DialogTemplate>();
	
	@RequestMapping(value = "/template")
	@ResponseBody
	public DialogTemplate template(HttpServletRequest request) throws Exception {
		if (Utils.isEmpty(dialogTemplates)) {
			// 模板获取地址
			DialogTemplate std = PubView.getStdTemplate();
			dialogTemplates.put(std.getId(), std);
			
			DialogTemplate alert = PubView.getAlertTemplate();
			dialogTemplates.put(alert.getId(), alert);
			
			DialogTemplate prong = PubView.getProngTemplate();
			dialogTemplates.put(prong.getId(), prong);
		}
		
		String tempId = request.getParameter("tempId");
		return dialogTemplates.get(tempId);
	}

	@RequestMapping(value = "/index")
	@ResponseBody
	public Object index(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		String anchor=request.getParameter("anchor");
		View view = DemoView.buildIndexView(loginUser,anchor);
		return view;
	}
	@RequestMapping(value = "/editUser")
	@ResponseBody
	public Object editUser(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		DialogCommand dialog = new DialogCommand("editUser", "std", "个人信息",  DialogCommand.WIDTH_SMALL, DialogCommand.HEIGHT_SMALL, null, "");
		dialog.setNode(DemoView.buildEditUserView(loginUser));
		return dialog;
	}
	@RequestMapping(value = "/saveUser")
	@ResponseBody
	public Object saveUser(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		bind(request,loginUser);
		UserService userService = FrameworkHelper.getBean(UserService.class);
		userService.update(loginUser);
		
		CommandGroup cg = new CommandGroup();
//		cg.setPath("/index");
		ReplaceCommand uc = new ReplaceCommand();
		cg.add(uc);
		Widget<?> loginBar = DemoView.buildLoginBar(loginUser);
		uc.setNode(loginBar);
		cg.add(DemoView.getAlertInfo("保存成功!"));
		cg.add(new JSCommand("dfish.close('editUser');"));
		return cg;
	}
	@RequestMapping(value = "/editPwd")
	@ResponseBody
	public Object editPwd(HttpServletRequest request) throws Exception {
		DialogCommand dialog = new DialogCommand("editPwd", "std", "修改密码",  DialogCommand.WIDTH_SMALL, DialogCommand.HEIGHT_SMALL, null, "");
		dialog.setNode(DemoView.buildEditPwdView());
		return dialog;
	}
	@RequestMapping(value = "/savePwd")
	@ResponseBody
	public Object savePwd(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		String oldPwd = request.getParameter("oldPwd");
		String password = request.getParameter("password");
		if(oldPwd.equals(loginUser.getUserPwd())){
			loginUser.setUserPwd(password);
			UserService userService = FrameworkHelper.getBean(UserService.class);
			userService.update(loginUser);
			
			CommandGroup cg = new CommandGroup();
			cg.add(DemoView.getAlertInfo("保存成功!"));
			cg.add(new JSCommand("dfish.close('editPwd');"));
			return cg;
		}else{
			return new TipCommand("原密码错误！").setSnap("oldPwd").setSnaptype("tb");
		}
		
		
	}
	
}
